package com.jida.tijian.service;

import com.jida.tijian.domain.Users;
import com.jida.tijian.dto.ResponseObject;
import com.jida.tijian.exception.TijianAPPException;
import com.jida.tijian.exception.TijianAPPExceptionEnum;
import com.jida.tijian.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;

    //    捣鬼一下
//    private int a = 10 / 0;

    /**
     * 处理登录请求
     */
    @Override
    public ResponseObject<Users> login(Users user) {
        ResponseObject<Users> responseObject = null;
        System.out.println(user);
        // 新增：校验参数合法性
        if (user == null) {
            return new ResponseObject<>(500, "用户对象不能为空");
        }
        if (user.getUserId() == null || user.getUserId().trim().isEmpty()) {
            return new ResponseObject<>(500, "用户ID（手机号）不能为空");
        }
        if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
            return new ResponseObject<>(500, "密码不能为空");
        }
        //1.先查
        Users result = usersMapper.selectByPrimaryKey(user.getUserId());


        //2.看有没有
        if (result == null) {
            //告诉前端
            responseObject = new ResponseObject<>(201, "手机号尚未注册");
            //return
            return responseObject;
        }
        System.out.println(result);
        //3.比对密码
        if (!result.getPassword().equals(user.getPassword())) {
            //告诉前端
            responseObject = new ResponseObject<>(202, "密码错误");
            //return
            return responseObject;
        }
        System.out.println(result.getUserId() + result.getPassword());
        //result 是 数据对象 有密码和 账号id
        responseObject = new ResponseObject<>(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC, result);

        return responseObject;
    }
}

//    @Override
//    public ResponseObject<Users> login(Users user) {
//        System.out.println("service");
//        ResponseObject<Users> responseObject = null;
//        System.out.println(user.getUserId() + " " + user.getPassword());
//        //1.先查
//        Users result = usersMapper.selectByPrimaryKey(user.getUserId());
//
//
//        //2.看有没有
/// /        if (result == null) {
/// /            //告诉前端
/// /            throw new TijianAPPException(TijianAPPExceptionEnum.PHONE_NOT_REGISTER.getCode(),
/// /                    TijianAPPExceptionEnum.PHONE_NOT_REGISTER.getMessage());
/// ///                    responseObject = new ResponseObject<>(201, "手机号尚未注册");
/// /            //return
/// ///            return responseObject;
/// /        }
//        System.out.println(result.getUserId() + " " + result.getPassword());
//        //3.比对密码
//        if (!result.getPassword().equals(user.getPassword())) {
//            //告诉前端
////            responseObject = new ResponseObject<>(202, "密码错误");
//            throw new TijianAPPException(
//                    TijianAPPExceptionEnum.PASSWORD_IS_WRONG.getCode(),
//                    TijianAPPExceptionEnum.PASSWORD_IS_WRONG.getMessage()
//            );
//            //return
////            return responseObject;
//        }
//        System.out.println(result.getUserId() + result.getPassword());
//        //result 是 数据对象 有密码和 账号id
//        responseObject = new ResponseObject<>(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC, result);
//
//        return responseObject;
//    }
//}